<?php
defined ('_ALLOW') or die ('Access denied');
class product {
	var $id;
	var $sid;
	var $catid;
	var $code;
	var $title;
	var $tag;
	var $introtext;
	var $image="";
	var $order;
	var $dongia;
	var $fdongia;
	var $tongtien;
	var $ftongtien;
	var $dai;
	var $rong;
	var $tongdientich;
	var $duongrong;
	var $diachi;
	var $id_phaply;
	var $vip;
	var $ttime;
	var $fttime;
	var $lastupdate;
	var $flastupdate;
	var $id_huong;
	var $id_giaodich;
	var $lienhe;
	var $nums;
	var $user;
	function Doc_danh_sach ($catid, $count, $begin=0) {
		$where = "";
		if ($catid != "") {
			$where = "catid = $catid";
		}
		return $this->Fill($where, $count, $begin);
	}
	function CountFill ($strwhere) {
		global $csdl;
		$strsql = "SELECT COUNT(*) as count FROM products";
		if ($strwhere != "")
			$strsql .= " WHERE " . $strwhere;
		$rowsdb = $csdl->Truyvan($strsql);
		$rowdb = mysql_fetch_array($rowsdb,MYSQL_ASSOC);
		$count = $rowdb['count'];
		return $count;
	}
	function Fill ($strwhere, $count, $begin=0) {
		global $csdl;
//		$strformatdate = "%d/%m/%Y";
		$strformatdate = "%H:%i %d/%m/%Y";
		//$strformatdate2 = "%d/%m/%Y";
		$strsql = "SELECT *, DATE_FORMAT(time,'$strformatdate') as ftime, DATE_FORMAT(lastupdate,'$strformatdate') as flastupdate, format(dongia,0) as fdongia, format(tongtien,0) as ftongtien FROM products";
		if ($strwhere!="") {
			$strsql .= " WHERE " . $strwhere;
		}
		$strsql .= " ORDER BY vip   DESC, lastupdate DESC, id DESC, iorder DESC";
		$strsql .= " LIMIT $begin, $count";
		$rowsdb = $csdl->Truyvan ($strsql);
		$i=0;
		$this->nums=0;
		if ($rowsdb==false) return null;
		while ($rowdb = mysql_fetch_array($rowsdb,MYSQL_ASSOC)) {
			$rowarray[$i] = $this->Khoi_tao($rowdb);
			$i++;
		}
		if ($i==0) return null;
		$this->nums = $i;
		return $rowarray;
	}
	function CheckSearch ($keyword) {
		if ($keyword=="") return false;
		$strwhere = "";
		$strwhere .= "(";
		$strwhere .= "lower(title) LIKE lower('%" . $keyword . "%') ";
		$strwhere .= "OR lower(introtext) LIKE lower('%" . $keyword . "%') ";
		$strwhere .= ")";
		return $this->Fill($strwhere);
	}
	function Doc_danh_sach_s ($sid="", $count, $begin=0) {
		$where = "";
		if ($sid != "") {
			$where = "sid = $sid";
		}
		return $this->Fill($where, $count, $begin);
	}
	function Doc ($id) {
		$rowarray = $this->Fill ("id=$id", 1, 0);
		if ($rowarray==null)
			return null;
		else
			return $rowarray[0];
	}
	function Khoi_tao ($rowdb) {
		$kq = new product;
		$kq->id = $rowdb['id'];
		$kq->sid = $rowdb['sid'];
		$kq->catid = $rowdb['catid'];
		$kq->code = $rowdb['code'];
		$kq->title = $rowdb['title'];
		$kq->tag = $rowdb['tag'];
		$kq->introtext = $rowdb['introtext'];
		$kq->image = $rowdb['image'];
		$kq->order = $rowdb['iorder'];
		$kq->dongia = $rowdb['dongia'];
		$kq->fdongia = $rowdb['fdongia'];
		$kq->tongtien = $rowdb['tongtien'];
		$kq->ftongtien = $rowdb['ftongtien'];
		$kq->dai = $rowdb['dai'];
		$kq->rong = $rowdb['rong'];
		$kq->tongdientich = $rowdb['tongdientich'];
		$kq->duongrong = $rowdb['duongrong'];
		$kq->diachi = $rowdb['diachi'];
		$kq->id_phaply = $rowdb['id_phaply'];
		$kq->vip = $rowdb['vip'];
		$kq->ttime = $rowdb['time'];
		$kq->lastupdate = $rowdb['lastupdate'];
		$kq->fttime = $rowdb['ftime'];
		$kq->flastupdate = $rowdb['flastupdate'];
		$kq->id_huong = $rowdb['id_huong'];
		$kq->id_giaodich = $rowdb['id_giaodich'];
		$kq->lienhe = $rowdb['lienhe'];
		$kq->user = $rowdb['user'];
		return $kq;
	}
	function Xoa () {
		checkpermission("grantproductcat",$this->catid) or die ('Access denied');
		global $csdl;
		$csdl->Xoa("products","id=$this->id");
	}
	function DocForm () {
		global $cfg_live_site;
		$this->id = $_REQUEST['id'];
		$this->sid = $_REQUEST['sid'];
		$this->catid = $_REQUEST['catid'];
		$this->code = $_REQUEST['code'];
		$this->title = $_REQUEST['title'];
		$this->tag = $_REQUEST['tag'];
		$this->introtext = $_REQUEST['introtext'];
		$this->image = $_REQUEST['image'];
		
		$this->dongia = 0;
		if ($_REQUEST['dongia'] != "") {
			$this->dongia = $_REQUEST['dongia'];
			$this->dongia = str_replace(",","",$this->dongia);
		}
	
		
		$this->dai = 0;
		if ($_REQUEST['dai'] != "") {
			$this->dai = $_REQUEST['dai'];
			$this->dai = str_replace(",","",$this->dai);
		}
		
		$this->rong = 0;
		if ($_REQUEST['rong'] != "") {
			$this->rong = $_REQUEST['rong'];
			$this->rong = str_replace(",","",$this->rong);
		}

		
		$this->duongrong = 0;
		if ($_REQUEST['duongrong'] != "") {
			$this->duongrong = $_REQUEST['duongrong'];
			$this->duongrong = str_replace(",","",$this->duongrong);
		}
		$this->diachi = $_REQUEST['diachi'];
		$this->id_phaply = $_REQUEST['id_phaply'];

		$this->vip = $_REQUEST['vip'];
		
		$this->id_huong = $_REQUEST['id_huong'];
		$this->id_giaodich = $_REQUEST['id_giaodich'];
		
	
		
		
		
		$this->ttime = $_REQUEST['time'];
		$this->ttime = substr($this->ttime,0,10);
		$tnow = getdate();
		$hour = $tnow['hours'];
		$minute = $tnow['minutes'];
		$second = $tnow['seconds'];
		$this->ttime .= " $hour" . ":" . $minute . ":" . $second;
		$this->lastupdate = $this->ttime;
		
		
		
		$this->lienhe = $_REQUEST['lienhe'];
		$this->user = $_REQUEST['user'];
		
	}
	function GetMaxOrder () {
		$strwhere = "iorder>=all";
		$strwhere .= " (SELECT iorder FROM products)";
		$rowarray = $this->Fill ($strwhere, 1, 0);
		if ($rowarray==null) return 0;
		return $rowarray[0]->order;
	}
	function ChangeOrder ($neworder) {
		checkpermission("grantproductcat",$this->catid) or die ('Access denied');
		global $csdl;
		$strsql = "UPDATE `products` SET ";
		$strsql .= "`iorder` = $neworder ";
		$strsql .= "WHERE `id`=$this->id";
		$csdl->Ghi($strsql);
	}
	function Ghi () {
		global $csdl;
		checkpermission("grantproductcat",$this->catid) or die ('Access denied');
		if ($this->id > -1) {
			$tdt = $this->dai*$this->rong;
			$thanhtien = $tdt*$this->dongia;
			$strsql = "UPDATE `products` SET ";
			$strsql .= "`sid`=$this->sid, ";
			$strsql .= "`catid`=$this->catid, ";
			$strsql .= "`code` = '$this->code', ";
			$strsql .= "`title`='$this->title', ";
			$strsql .= "`tag` = '$this->tag', ";
			$strsql .= "`introtext`='$this->introtext', ";
			$strsql .= "`image`='$this->image', ";
			$strsql .= "`dongia`=$this->dongia, ";
			$strsql .= "`tongtien`=$thanhtien, ";
			$strsql .= "`dai`=$this->dai, ";
			$strsql .= "`rong`=$this->rong, ";
			$strsql .= "`tongdientich`=$tdt, ";
			$strsql .= "`duongrong`=$this->duongrong, ";
			$strsql .= "`diachi`='$this->diachi', ";
			$strsql .= "`id_phaply`=$this->id_phaply, ";
			$strsql .= "`vip`=$this->vip, ";
			$strsql .= "`lastupdate` = '$this->lastupdate', ";
			$strsql .= "`id_huong`=$this->id_huong, ";
			$strsql .= "`id_giaodich`=$this->id_giaodich, ";
			$strsql .= "`lienhe`='$this->lienhe', ";
			$strsql .= "`user`='$this->user' ";
			$strsql .= "WHERE `id`=$this->id";
		}
		else {
			$neworder = $this->GetMaxOrder() + 1;
			$tdt = $this->dai*$this->rong;
			$thanhtien = $tdt*$this->dongia;
			$strsql = "INSERT INTO `products` (`sid`, `catid`, `code`, `title`, `tag`, `introtext`, `image`, `iorder`, `dongia`, `tongtien`, `dai`, `rong`, `tongdientich`, `duongrong`, `diachi`, `id_phaply`, `vip`, `time`, `lastupdate`, `id_huong`, `id_giaodich`, `lienhe`, `user`) ";
			$strsql .= "VALUES ($this->sid, $this->catid, '$this->code', '$this->title', '$this->tag', '$this->introtext','$this->image', $neworder, $this->dongia, $thanhtien, $this->dai, $this->rong, $tdt, $this->duongrong, '$this->diachi', $this->id_phaply, $this->vip, '$this->ttime', '$this->lastupdate', $this->id_huong, $this->id_giaodich, '$this->lienhe', '$this->user')";
		}
//echo $strsql;
		$csdl->Ghi($strsql);
	}
	function Out_Image ($maxwidth) {
		global $cfg_image_root;
		$objimage = new image($this->image);
		$sizeimage = $objimage->getsize();
		if (!is_array($sizeimage)) return;
		$fullwidth = $sizeimage[0];
		$scalewidth = $maxwidth;
		if ($fullwidth < $scalewidth)
			$scalewidth = $fullwidth;
		$imgsrc = $cfg_image_root . $this->image;

		?>
		<img src="<?php echo $imgsrc; ?>" width="<?php echo $scalewidth; ?>" border="0" />
		<?php
	}
	function Get_Url () {
		$url = "index.php?module=com_product&task=view&id=$this->id";
		if (isset($_GET['curPage'])) {
			$tcurPage = $_GET['curPage'];
			$url .= "&curPage=" . $tcurPage;
		}
		if (isset($_GET['Itemid'])) {
			$titemid = $_GET['Itemid'];
			$url .= "&Itemid=" . $titemid;
		}
		return $url;
	}
	function Get_Url2 () {
		$url = "index.php?module=com_product&task=view&id=$this->id";
		if (isset($_GET['curPage'])) {
			$tcurPage = $_GET['curPage'];
			$url .= "&curPage=" . $tcurPage;
		}
		return $url;
	}
	function Get_Url3 () {
		$url = "index.php?module=com_product&task=view&id=$this->id";
		if (isset($_GET['Itemid'])) {
			$titemid = $_GET['Itemid'];
			$url .= "&Itemid=" . $titemid;
		}
		return $url;
	}
	
	function Out_Title_Link ($leadding="") {
		$title = $this->Get_Title();
		$url = $this->Get_Url4();
		$size = 400;
		$objimage = new image($this->image);
		$sizeimage = $objimage->getsize();
		if ($size < $sizeimage[0])
			$size = $sizeimage[0];
		?>
		<a href="<?php echo $url; ?>" target="_blank" onclick="javascript: return openBox(<?php
		echo $size; ?>,600,'yes','no',0,0,'<?php echo $url; ?>',window);return false;"><?php echo $title; ?></a>
		<?php
	}
	function Get_Title () {
		$title = $this->title;
		if($this->vip == 1)
			$title .= "<span style='font-family:Tahoma; color:#ff0000; font-size:11px; font-weight:bold;'>  (VIP)</span>";
		return $title;
	}
	function Get_Intro () {
		global $lang;
		$intro = $this->introtext;
		if ($lang == "en") $intro = $this->introtext_en;
		return $intro;
	}
	function Get_Url4 () {
		$url = "index2.php?module=com_product&task=view&id=$this->id";
		return $url;
	}
	function Out_TheHien_TT ($maxwidth) {
		global $lang;
		global $currency;
		$readmore="Chi tiết >>";
		$intro=$this->Get_Intro();
		$title=$this->Get_Title();
		if ($lang=="en") {
			$readmore = "Read more";
		}
		$url = $this->Get_Url4();
		$size = 400;
		$objimage = new image($this->image);
		$sizeimage = $objimage->getsize();
		if ($size < $sizeimage[0])
			$size = $sizeimage[0];
		?>
		<div align="center" >
		<a href="<?php echo $url; ?>" target="_blank" onclick="javascript:  openBox(<?php
		echo $size; ?>,500,'yes','no',0,0,'<?php echo $url; ?>',window); return false;">
            <?php $this->Out_Image($maxwidth); ?>
        </a>
		</div>
		<div class="title_product" align="center"><?php $this->Out_Title_Link(); ?></div>
							
		
               
		
		<?php
	}
	
	function Out_PartForm () {
		$strquality = "Số lượng";
		global $lang;
		$productid = $this->id;
		$productname = $this->Get_Title();
		if ($lang=="en") {
			$strquality = "Quantity";
		}
		$productprice = $this->price;
		$productfprice = $this->fprice;
		$formname = "formproduct".$this->id;
		?>
		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="table_formproduct">
		<form name="<?php echo $formname; ?>" id="<?php echo $formname; ?>" action="index.php" method="GET" onsubmit="return CheckSubmitCart(this)" >
		<tr>
		<td class="lable" width="60"><img src="images/icon.gif" /> <?php echo $strquality; ?></td>
		<td width="60"><input type="text" name="productquanlity" class="text_area" size="5" style="text-align:right;" /></td>
		<td><input type="image" src="images/img_cart.jpg" /></td>
		</tr>
		<input type="hidden" name="productid" value="<?php echo $productid; ?>" />
		<input type="hidden" name="productname" value="<?php echo $productname; ?>" />
		<input type="hidden" name="productprice" value="<?php echo $productprice; ?>" />
		<input type="hidden" name="productfprice" value="<?php echo $productfprice; ?>" />
		<input type="hidden" name="task" value="add" />
        <input type="hidden" name="module" value="com_shoppingcart" />
		</form>
		</table>
		<?php
	}
	
	function Out_TheHien ($maxwidth) {
		$title=$this->Get_Title();
		$intro = $this->Get_Intro();
		
		include_once("class/cproductcategory.php");
		global $objproductcategory;
		include_once("class/cphaply.php");
		global $objphaply;
//		include_once("class/chuong.php");
//		include_once("class/cgiaodich.php");
//		
		$loaibds = $objproductcategory->Fill("id = $this->catid");
		$phaply = $objphaply->Fill("id = $this->id_phaply");
		global $lang;
		global $currency;
		
		
		

		?>
		
		<div style="padding-top:10px; padding-bottom:10px;  " >
        	<table width="100%" cellpadding="0" cellspacing="0" border="0">
            <tr>
            	<td width="30%">
					<table width="100%" cellpadding="0" cellspacing="0" border="0">
						<tr>
							<td>
								<div align="center">
									<?php 
										if($this->image != "")
											$this->Out_Image($maxwidth); 
										else
										{
											global $cfg_image_root;
											$imgsrc = $cfg_image_root . "logo_nc.jpg";
											
									?>
									<img src="<?php echo $imgsrc; ?>" width="91" border="0" />
									<?php
										}
									?>
								</div>
								<div align="center" style="font-family:Tahoma; font-size:12px; font-weight:bold; color:#0033FF;"><?php echo $this->code; ?></div>
							</td>
						</tr>
					</table>
				</td>
				<?php
					$size=20;
				?>
				<td width="70%" style="padding-bottom:10px;" >
					<table width="100%" cellpadding="0" cellspacing="0" border="0" >
						<tr >
							<td class="noidung" align="left" width="<?php echo $size;?>%">Loại BDS </td>
							<td align="left" class="noidung" width="<?php echo 100-$size;?>%">: <?php  echo $loaibds[0]->title;?></td>
						</tr>
						<tr>
							<td align="left" width="<?php echo $size;?>%" class="noidung">Diện tích </td>
							<td class="noidung" align="left" width="<?php echo 100-$size;?>%">: <?php  echo $this->rong;?> x <?php  echo $this->dai;?> = <?php  echo $this->tongdientich;?> m &sup2;</td>
						</tr>
						<tr>
							<td align="left" class="noidung" width="<?php echo $size;?>%">Đường rộng </td>
							<td align="left" class="noidung" width="<?php echo 100-$size;?>%">: <?php  echo $this->duongrong;?> m</td>
						</tr>
						<tr>
							<td align="left" class="noidung" width="<?php echo $size;?>%">Hướng </td>
							<td align="left" class="noidung" width="<?php echo 100-$size;?>%">: <?php 
							include_once("class/chuong.php");
							global $objhuong;
							$objhuong = $objhuong->Doc($this->id_huong);
							echo $objhuong->title;
							?></td>
						</tr>
						<tr>
							<td align="left" width="<?php echo $size;?>%" class="noidung">Địa chỉ </td>
							<td align="left" width="<?php echo 100-$size;?>%" class="noidung">: <?php  echo $this->diachi;?></td>
						</tr>
						<tr>
							<td align="left" width="<?php echo $size;?>%" class="noidung">TT Pháp lý </td>
							<td align="left" width="<?php echo 100-$size;?>%" class="noidung">: <?php  echo $phaply[0]->title;?></td>
						</tr>
						<tr>
							<td align="left" width="<?php echo $size;?>%" class="noidung">Đơn giá/m &sup2; </td>
							<td align="left" width="<?php echo 100-$size;?>%" class="noidung">: <?php  echo $this->fdongia;?> VNĐ</td>
						</tr>
						<tr>
							<td align="left" width="<?php echo $size;?>%" class="noidung">Tổng T.tiền </td>
							<td align="left" width="<?php echo 100-$size;?>%" class="noidung">: <?php  echo $this->ftongtien;?> VNĐ</td>
						</tr>
						<tr>
							<td align="left" width="<?php echo $size;?>%" class="noidung">Liên hệ </td>
							<td align="left" width="<?php echo 100-$size;?>%" class="noidung">: <?php  echo $this->lienhe;?></td>
						</tr>
					</table>
				</td>
            </tr> 
			<?php
				if($intro != "")
				{
			?>
			<tr>
				<td colspan="2" class="noidung" style="padding-left:5px; padding-right:5px; padding-bottom:5px; padding-top:5px; text-align:justify; font-family:Tahoma; font-size:12px; font-weight:bold; color:#0033FF; text-decoration:underline; border-top:1px #999999 dotted;">
				Một số chi tiết khác :
				</td>
			</tr>      
			<tr >
				<td colspan="2" class="noidung" style="padding-left:5px; padding-right:5px; text-align:justify; padding-bottom:5px;">
					<?php
						echo $intro;
					?>
				</td>
			</tr>      
			<?php
				}
			?>
		</table>
		</div>
        
		<?php
	}
	function Out_TheHien_Marquee () {
		$title = $this->Out_Title();
		$intro = $this->Out_Intro();
		$url = $this->Get_Url4();
		?>
		<a href="<?php echo $url; ?>" class="title_product" title="$intro"><?php echo $title; ?></a>
		<?php
	}
	function Out_TheHien_TT_Foot ($maxwidth) {
		$this->Out_TheHien_TT($maxwidth);
	}
};
global $objproduct; $objproduct = new product;
?>